package com.bayihouse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.Occupant;
import com.bayihouse.model.entity.Tempermit;
import com.bayihouse.model.request.occupant.OccupantUpdateRequest;
import com.bayihouse.model.request.tempermit.TempermitAddRequest;
import com.bayihouse.model.request.tempermit.TempermitQueryRequest;
import com.bayihouse.model.request.tempermit.TempermitUpdateRequest;
import com.bayihouse.model.vo.TempermitVO;
import com.bayihouse.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/tempermit")
@Slf4j
public class TempermintController {


    @Resource
    private TempermitService tempermitService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param tempermitAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Long> addTempermit(@RequestBody TempermitAddRequest tempermitAddRequest, HttpServletRequest request) {
        if (tempermitAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tempermit tempermit = new Tempermit();
        if (tempermitAddRequest.getDealtime() != null) {
            Date date = new Date();
            long time = tempermitAddRequest.getDealtime().getTime();
            if (time < date.getTime()) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "截止时间不合理");
            }
        }
        BeanUtils.copyProperties(tempermitAddRequest, tempermit);
        boolean result = tempermitService.save(tempermit);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(tempermit.getId());
    }


    @PostMapping("/update")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> updateOccupant(@RequestBody TempermitUpdateRequest tempermitUpdateRequest,
                                                HttpServletRequest request) {
        System.out.println(tempermitUpdateRequest);
        if (tempermitUpdateRequest == null || tempermitUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tempermit tempermit = new Tempermit();
        if (tempermitUpdateRequest.getDealtime() != null) {
            Date date = new Date();
            long time = tempermitUpdateRequest.getDealtime().getTime();
            if (time < date.getTime()) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "截止时间不合理");
            }
        }
        BeanUtils.copyProperties(tempermitUpdateRequest, tempermit);
        boolean result = tempermitService.updateById(tempermit);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }


    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> deleteTempermit(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = tempermitService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }


    /**
     * 根据 id 获取用户（仅管理员）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<TempermitVO> getTempermitById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tempermit tempermit = tempermitService.getById(id);
        ThrowUtils.throwIf(tempermit == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(TempermitVO.objToVo(tempermit));
    }

    public BaseResponse<Tempermit> getTempermitById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tempermit tempermit = tempermitService.getById(id);
        ThrowUtils.throwIf(tempermit == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(tempermit);
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<TempermitVO> getTempermitVOById(long id, HttpServletRequest request) {
        BaseResponse<Tempermit> response = getTempermitById(id);
        Tempermit tempermit = response.getData();
        return ResultUtils.success(tempermitService.getTempermitVO(tempermit));
    }

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param tempermitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<Tempermit>> listTempermitByPage(@RequestBody TempermitQueryRequest tempermitQueryRequest,
                                                             HttpServletRequest request) {
        long current = tempermitQueryRequest.getCurrent();
        long size = tempermitQueryRequest.getPageSize();
        Page<Tempermit> tempermitPage = tempermitService.page(new Page<>(current, size),
                tempermitService.getQueryWrapper(tempermitQueryRequest));
        return ResultUtils.success(tempermitPage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param tempermitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<TempermitVO>> listTempermitVOByPage(@RequestBody TempermitQueryRequest tempermitQueryRequest,
                                                                 HttpServletRequest request) {
        if (tempermitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = tempermitQueryRequest.getCurrent();
        long size = tempermitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Tempermit> tempermitPage = tempermitService.page(new Page<>(current, size),
                tempermitService.getQueryWrapper(tempermitQueryRequest));
        System.out.println(tempermitPage.getRecords());
        Page<TempermitVO> tempermitVOPage = new Page<>(current, size, tempermitPage.getTotal());
        List<TempermitVO> tempermitVOList = tempermitService.getTempermitVOList(tempermitPage.getRecords());
        tempermitVOPage.setRecords(tempermitVOList);
        System.out.println(tempermitVOPage.getRecords());
        return ResultUtils.success(tempermitVOPage);
    }


}



